package ro.gii.auctionHouse.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The Class Util.
 */
public class Util {

	/**
	 * The Constant PASSWORD_ENCRYPTION_MD5.
	 */
	private static final String PASSWORD_ENCRYPTION_MD5 = "MD5";

	/**
	 * The Constant CHARSET_ENCRYPTION_ISO.
	 */
	private static final String CHARSET_ENCRYPTION_ISO = "iso-8859-1";

	/**
	 * The Constant LOGGER.
	 */
	private static final Logger LOGGER = LoggerFactory.getLogger(Util.class);

	/**
	 * Encrypt md5 password.
	 *
	 * @param text the text
	 * @return the string
	 * @throws NoSuchAlgorithmException the no such algorithm exception
	 * @throws UnsupportedEncodingException the unsupported encoding exception
	 */
	public static String encryptMD5Password(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
		MessageDigest md;
		md = MessageDigest.getInstance(PASSWORD_ENCRYPTION_MD5);
		md.update(text.getBytes(CHARSET_ENCRYPTION_ISO), 0, text.length());
		byte[] md5 = md.digest();
		return convertedToHex(md5);
	}

	/**
	 * Decrypt md5 password.
	 *
	 * @param password the password
	 * @return the string
	 */
	public static String decryptMD5Password(String password) {

		byte[] defaultBytes = password.getBytes();
		MessageDigest algorithm = null;
		try {
			algorithm = MessageDigest.getInstance(PASSWORD_ENCRYPTION_MD5);
		} catch (NoSuchAlgorithmException e) {
			LOGGER.debug("ERROR:could not find the algorithm encryption !", e);
		}
		StringBuffer hexString = new StringBuffer();

		if (algorithm != null) {
			algorithm.reset();
			algorithm.update(defaultBytes);
			byte messageDigest[] = algorithm.digest();

			for (int i = 0; i < messageDigest.length; i++) {
				String hexCode = Integer.toHexString(0xFF & messageDigest[i]);
				if (hexCode.length() == 1) {
					hexCode = "0".concat(hexCode);
				}
				hexString.append(hexCode);
			}
		}
		return hexString.toString();

	}

	/**
	 * Converted to hex.
	 *
	 * @param data the data
	 * @return hex string
	 */
	private static String convertedToHex(byte[] data) {
		StringBuffer buf = new StringBuffer();

		for (int i = 0; i < data.length; i++) {
			int halfOfByte = (data[i] >>> 4) & 0x0F;
			int twoHalfBytes = 0;

			do {
				if ((0 <= halfOfByte) && (halfOfByte <= 9)) {
					buf.append((char) ('0' + halfOfByte));
				} else {
					buf.append((char) ('a' + (halfOfByte - 10)));
				}

				halfOfByte = data[i] & 0x0F;

			} while (twoHalfBytes++ < 1);
		}
		return buf.toString();
	}


	/**
	 * Gets the current date.
	 *
	 * @return current date
	 */
	public static Date getCurrentDate() {
		final Calendar calendar = Calendar.getInstance();
		return calendar.getTime();
	}

	/**
	 * Gets the current date.
	 *
	 * @param date the date
	 * @return current date
	 */
	public static Date getDateWithoutTime(Date date) {
		return getCalendarWithDateWithoutTime(date).getTime();
	}

	/**
	 * Gets the current date.
	 *
	 * @return current date
	 */
	public static Date getCurrentDateWithoutTime() {
		return getCalendarWithCurrentDateWithoutTime().getTime();
	}

	/**
	 * Gets the current date.
	 *
	 * @return current date
	 */
	public static Calendar getCalendarWithCurrentDateWithoutTime() {
		return getCalendarWithDateWithoutTime(null);
	}

	/**
	 * Gets the current date.
	 *
	 * @param date the date
	 * @return current date
	 */
	public static Calendar getCalendarWithDateWithoutTime(Date date) {
		final Calendar calendar = Calendar.getInstance();
		if (date != null) {
			calendar.setTime(date);
		}
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar;
	}

	/**
	 * Gets the date format.
	 *
	 * @param date the date
	 * @param dateFormat the date format
	 * @return the date format
	 */
	public static String getDateFormat(Date date, String dateFormat) {
		SimpleDateFormat df = new SimpleDateFormat(dateFormat);
		return df.format(date);
	}

	/**
	 * Checks if is same date without time.
	 *
	 * @param a the a
	 * @param b the b
	 * @return true, if is same date without time
	 */
	public static boolean isSameDateWithoutTime(Date a, Date b) {
		Calendar cal1 = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		cal1.setTime(a);
		cal2.setTime(b);

		if (cal1.get(Calendar.YEAR) != cal2.get(Calendar.YEAR) || cal1.get(Calendar.MONTH) != cal2.get(Calendar.MONTH) || cal1.get(Calendar.DAY_OF_MONTH) != cal2.get(Calendar.DAY_OF_MONTH)) {
			return false;
		}

		return true;

	}

}